package com.sss.demo.web.log;

import javax.servlet.http.HttpSession;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import com.sss.demo.GlobalConstants;
import com.sss.demo.db.pojo.OperateLogVo;
import com.sss.demo.db.pojo.UserVo;
import com.sss.demo.web.methodLog;
import com.sss.demo.web.service.IOperateLogService;
@Aspect
@Component
public class OperateLogAspect {

	@Autowired
	private IOperateLogService logService;
	


	public IOperateLogService getLogService() {
		return logService;
	}

	public void setLogService(IOperateLogService logService) {
		this.logService = logService;
	}

	// 标注该方法体为后置通知，当目标方法执行成功后执行该方法体
	@After("within(com.sss.demo.web..*)  && @annotation(rl)")
	public void addLogSuccess(JoinPoint jp, methodLog rl) {
		//这里session应当这样获取
		 HttpSession session = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest().getSession();  
		UserVo actor = ((UserVo)session.getAttribute(
				GlobalConstants.SESSION_ACTOR_KEY));
		if(actor==null){
			 actor=new UserVo();
			 actor.setMail("Tommy@sina.com");
			 actor.setName("Tommy");
		}
		OperateLogVo logvo = new OperateLogVo();
		logvo.setAuthor(actor.getMail());
		logvo.setOperateDesc(rl.operateDesc());
		logvo.setOperateType(rl.operateType());

		if (actor == null) {
		logvo.setAuthor("nobody");
		}

		logService.insertLog(logvo);
	}
}
